package com.infore.water.controller;

import com.infore.water.service.AlarmService;
import com.infore.water.service.StationService;
import com.infore.water.util.ListObject;
import com.infore.water.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;

/**
 * @author :wk
 */
@RestController
public class StationControl {

    @Autowired
    private StationService stationService;

    @Autowired
    private AlarmService alarmService;

    /**
     * 获取站点树
     * @param userId 用户id
     * @return ListObject
     */
    @RequestMapping("/getStationTree")
    public ListObject getStationTree(@RequestHeader(value = "currentId")String userId){
        return ResponseUtil.queryListObject(stationService.queryStationTree(userId),"获取站点树成功");
    }

    /**
     * 获取站点实时排名
     * @param userId 用户id
     * @param pageNum 页码
     * @param pageSize 页大小
     * @param sortType 排序
     * @return ListObject
     */
    @RequestMapping("/getStationRanking")
    public ListObject getStationRanking(@RequestHeader(value = "currentId")String userId,
                                        @RequestParam(value = "pageNum",required = false)String pageNum,
                                        @RequestParam(value = "pageSize",required = false)String pageSize,
                                        @RequestParam(value = "sortType",required = false)String sortType){
        return ResponseUtil.queryListObject(stationService.getStationRanking(userId,pageNum,pageSize,sortType),"获取站点树成功");
    }

    /**
     * 获取报警记录
     * @param siteId 站点id
     * @param stationId 监测点id
     * @param alarmType 报警类型
     * @return ListObject
     */
    @RequestMapping("/getAlarmRecord")
    public ListObject getAlarmRecord(@RequestParam(value = "siteId")String siteId,
                                     @RequestParam(value = "stationId")String stationId,
                                     @RequestParam(value = "alarmType")String alarmType,
                                     @RequestParam(value = "pageNum")String pageNum,
                                     @RequestParam(value = "pageSize")String pageSize) throws ParseException{
        return ResponseUtil.queryListObject(alarmService.getAlarmRecord(siteId,stationId,alarmType,pageNum,pageSize),"成功");
    }

    /**
     * 删除报警记录
     * @param alarmId 报警记录id
     * @return ListObject
     */
    @RequestMapping("/deleteAlarmRecord")
    public ListObject deleteAlarmRecord(@RequestParam("alarmId")String alarmId){
        alarmService.deleteAlarmRecord(alarmId);
        return ResponseUtil.success();
    }

    /**
     * 获取报警规则列表
     * @param siteId 站点id
     * @param stationId 监测点id
     * @param alarmType 报警类型
     * @param pageNum 页码
     * @param pageSize 页大小
     * @return ListObject
     */
    @RequestMapping("/getStandardList")
    public ListObject getStandardList(@RequestParam(value = "siteId")String siteId,
                                      @RequestParam(value = "stationId")String stationId,
                                      @RequestParam(value = "alarmType")String alarmType,
                                      @RequestParam(value = "pageNum")String pageNum,
                                      @RequestParam(value = "pageSize")String pageSize){
        return ResponseUtil.queryListObject(alarmService.getStandardList(siteId,stationId,alarmType,pageNum,pageSize),"成功");
    }


    /**
     * 获取站点对应因子
     * @return ListObject
     */
    @RequestMapping("/getStationProject")
    public ListObject getStationProject(@RequestHeader(value = "currentId")String userId){
        return ResponseUtil.queryListObject(alarmService.getStationProject(userId),"成功");
    }

    /**
     * 获取报警人员
     * @return ListObject
     */
    @RequestMapping("/getAlarmPerson")
    public ListObject getAlarmPerson(){
        return ResponseUtil.queryListObject(alarmService.getAlarmPerson(),"成功");
    }

    /**
     * 新增或修改报警规则
     * @param alarmId 规则id
     * @param stationId 监测点id
     * @param alarmType 报警类型
     * @param alarmLevel 报警级别
     * @param alarmStep 报警方式
     * @param alarmPerson 报警人
     * @param workDay 周
     * @param time 时间
     * @param interval 间隔时间
     * @return ListObject
     */
    @RequestMapping("/setAlarmStandard")
    public ListObject setAlarmStandard(@RequestParam(value = "alarmId",required = false)String alarmId,
                                       @RequestParam(value = "stationId")String stationId,
                                       @RequestParam(value = "alarmType")String alarmType,
                                       @RequestParam(value = "alarmLevel",required = false)String alarmLevel,
                                       @RequestParam(value = "alarmStep",required = false)String alarmStep,
                                       @RequestParam(value = "alarmPerson")String alarmPerson,
                                       @RequestParam(value = "workDay")String workDay,
                                       @RequestParam(value = "time")String time,
                                       @RequestParam(value = "interval")String interval){
        int num = alarmService.setAlarmStandard(alarmId,stationId,alarmType,alarmLevel,alarmStep,alarmPerson,workDay,time,interval);
        if(num==-1){
            return ResponseUtil.error("同一个站点同一种规则只能添加一个!");
        }
        return ResponseUtil.success();
    }

    /**
     * 删除报警规则
     * @param alarmId 规则id
     * @return ListObject
     */
    @RequestMapping("/deleteAlarmStandard")
    public ListObject deleteAlarmStandard(@RequestParam("alarmId")String alarmId){
        alarmService.deleteAlarmStandard(alarmId);
        return ResponseUtil.success();
    }
}
